# -*- coding: UTF-8 -*-
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to, redirect

from hellopylons.lib.base import BaseController, render
from hellopylons.model import meta
from hellopylons.model import User
from hellopylons.model import UserType
from hellopylons.model.Right_Left import Left
from hellopylons.controllers import CustomController
import json


log = logging.getLogger(__name__)

class UserController(BaseController):
    def Index(self):
        c.Info = '请输入用户名和密码'
        return render('/User/Login.mako')
    
    def Login(self):
        objLeft = meta.Session.query(Left).all()
        print(objLeft[0].Right[0].Name)
        UserFilter = meta.Session.query(User)
        strUserName = request.params['UserName']
        strPassword = request.params['Password']
        strPreUrl = ''
        if 'preUrl' in session:
            strPreUrl = session['preUrl']
            del session['preUrl']
            #print(strPreUrl)
        objUser = UserFilter.join(User.userType).filter(User.UserName==strUserName).filter(User.Password==strPassword).all()
        print(objUser[0].userType.UserTypeName)
        if len(objUser) > 0 :
            session['LoginUser'] = objUser[0].UserName
            c.UserName = objUser[0].UserName
            c.Info = ''
            if strPreUrl == '':
                return render('/User/Wellcome.mako')
            else:
                redirect(strPreUrl)
        else:
            c.Info = '错误的用户名或者密码'
            return render('/User/Login.mako')
        
        
    def Wellcome(self):
        if 'LoginUser' in session:
            c.UserName = session['LoginUser']
        return render('/User/Wellcome.mako')
    
    def Logout(self):
        if 'LoginUser' in session:
            del session['LoginUser']
        redirect_to(controller='User', action='Index')
        
    def Register(self):
        c.Info = ''
        c.UserTypeID = 1
        c.UserName = ''
        if 'tmpValues' in session:
            tmpValues = session['tmpValues']
            if 'Info' in tmpValues:
                c.Info = tmpValues['Info']
            if 'UserName' in tmpValues:
                c.UserName = tmpValues['UserName']
            del session['tmpValues']
        return render('/User/Add.mako')
    
    def RegOver(self):
        #读取用户类型
        #InitUserType
        #还原用户信息
        UserFilter = meta.Session.query(User)
        strUserName = request.params['UserName']
        strPassword = request.params['Password']
        strPasswordCheck = request.params['PasswordCheck']
        objExistedUser = UserFilter.filter(User.UserName==strUserName).all()
        tmpValues = {}
        tmpValues['UserName'] = strUserName
        tmpValues['Info'] = ''
        if len(objExistedUser) > 0:
            tmpValues['Info'] = '该用户已存在'
            session['tmpValues'] = tmpValues
            redirect_to(controller='User', action='Register')
        if strPassword != strPasswordCheck:
            tmpValues['Info'] = '密码和确认密码不一致'
            session['tmpValues'] = tmpValues
            redirect_to(controller='User', action='Register')
        AddedUser = User(self.UserName, self.Password)
        meta.Session.add(AddedUser)
        meta.Session.commit()
        session['LoginUser'] = strUserName
        redirect_to(controller='User', action='Wellcome')
        
    def Test(self):
        return 'this is a test'
            
        
            
